/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.clientserver;

/**
 *
 * @author Mr.Sam
 */
import java.io.IOException;
import fr.ove.clientserver.*;

/**
*
*/
public class EchoServer {
    /**
    * The server.
    */
    private Server server;
    
    /**
    * The constructor.
    * @param portNumber the port number used on the host to run the server (instance).
    * @param maxSocket the maximum number of connections allowed to the server (instance).
    */
    public EchoServer(int portNumber, int maxConnection) throws IOException {
        server = new Server(portNumber, maxConnection);
        server.setConnectionHandler(new EchoConnectionHandler());
    }
    
    private static void usage() {
        System.out.println("java fr.ove.clientserver.EchoServer [portNumber]");
    }
    
    public static void main(String args[]) {
        EchoServer eServer;
        try {
        	switch (args.length) {
        		case 1 :
        			try {
        				eServer = new EchoServer(Integer.parseInt(args[0]), 300);
        			} catch (NumberFormatException nfe) {
        				System.err.println(nfe.getMessage());
        				nfe.printStackTrace(System.err);
        				usage();
        			}
        			break;
        		case 0 :
        			eServer = new EchoServer(8080, 300);
        			break;
        		default :
        			usage();
        			System.exit(0);
        	}
        }
        catch (IOException ioe) {
            System.out.println("Failed to run server");
            ioe.printStackTrace();
        }
    }
}